package com.nexus.plugin.security.service;

import com.nexus.core.auth.LoginUser;
import com.nexus.core.auth.LoginUserType;
import com.nexus.core.sys.dto.SysUserDTO;
import com.nexus.core.utils.ObjectUtils;
import com.nexus.service.sys.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

/**
 * @Date 2025/7/31 10:57
 * @Author luzhengning
 **/
@Slf4j
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private SysUserService userService;


    @Override
    public LoginUser loadUserByUsername(String username) {
        //查询用户名
        SysUserDTO sysUser = userService.findByLoginname(username);
        if(ObjectUtils.isEmpty(sysUser)) {
            throw new RuntimeException("用户名错误或不存在");
        }
        LoginUser loginUser = new LoginUser();
        loginUser.setId(sysUser.getId());
        loginUser.setUser(sysUser);
        loginUser.setUserType(LoginUserType.SYS_USER);
        loginUser.setUsername(sysUser.getLoginName());
        loginUser.setPassword(sysUser.getPassword());
        return loginUser;
    }
}
